'use strict';
app.service('accountService',['$http','$q','utilService','$cookies',function($http,$q,utilService,$cookies){

    this.createSystemUser = function($scope){
        var roleIds=new Array();
        for(var i=0;i<$scope.roles.length;i++){
            if($scope.roles[i].isSelect==1) {
                roleIds.push($scope.roles[i].id);
            }
        }
        $scope.systemUser.roleIds=roleIds;

        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'system/user',
            data:$scope.systemUser
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }

    this.findById = function($scope){
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'system/user/'+ $scope.systemUser.systemUserId,
            params:{systemUserId:$scope.systemUser.systemUserId}
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错！");
        });
        return deferred.promise;
    }

    this.findBySessionId = function($scope){
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'system/user/find',
            params:{sessionId:$cookies.sessionId}
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错！");
        });
        return deferred.promise;
    }

    this.updateSystemUser = function($scope){
        var roleIds=new Array();
        for(var i=0;i<$scope.roles.length;i++){
            if($scope.roles[i].isSelect==1) {
                roleIds.push($scope.roles[i].id);
            }
        }
        $scope.systemUser.roleIds=roleIds;

        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'system/user/'+ $scope.systemUser.id,
            data:$scope.systemUser,
            method:'PUT'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }

    this.updateNowSystemUser = function($scope){
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'system/user/now/'+ $scope.systemUser.id,
            data:$scope.systemUser,
            method:'PUT'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }


    this.updateStatus=function($scope){
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + '/system/user/update/status/'+ $scope.systemId,
            data:{status:$scope.status},
            method:'PUT'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;

    }

    this.updateSystemUserStatus=function($scope){
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + '/system/user/status/'+ $scope.systemUserId,
            data:{status:$scope.status},
            method:'PUT'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;

    }

    var _this_=this;

    _this_.findAllBySystemUserid = function($scope){
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'role/account/'+ $scope.systemUserId
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错！");
        });
        return deferred.promise;
    }


    _this_.add = function($scope){
        var roleIds=new Array();
        for(var i=0;i<$scope.roles.length;i++){
            if($scope.roles[i].isSelect==1) {
                roleIds.push($scope.roles[i].id);
            }
        }


        var systemUserRoleAddVO={
            roleIds:roleIds,
            systemUserId:$scope.systemUserId
        }

        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'system/user/role',
            data:systemUserRoleAddVO
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }


    _this_.getAll = function($scope){
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'role/all/function',
            params:$scope.roleListVO
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错！");
        });
        return deferred.promise;
    }

    this.checkPhone = function($scope){
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'system/user/check/mobilePhone',
            params:{mobilePhone:$scope.systemUser.mobilePhone}
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错！");
        });
        return deferred.promise;
    }

    this.getMobilePhoneCode = function($scope,mobilePhone){
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'system/user/getMobilePhoneCode',
            params:{mobilePhone:mobilePhone}
        }).then(function(response){
            deferred.resolve(response);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错！");
        });
        return deferred.promise;
    }

    /**
     * 代理商帐号 -> 激活 || 取消激活
     * @ Dave.lai
     * @param $scope
     * @param systemId
     * @returns {*}
     */
    this.switchActivationStatus = function($scope, systemId, status) {
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'system/user/switchActivationStatus?systemId='+ systemId +'&status='+ status,
            method:'PUT'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }

}]);